package com.ybu.controller;

import com.ybu.common.Constants;
import com.ybu.common.JwtUtils;
import com.ybu.common.R;
import com.ybu.pojo.dto.AddressDTO;
import com.ybu.pojo.entity.Address;
import com.ybu.pojo.vo.AddressVO;
import com.ybu.pojo.vo.UserLoginVO;
import com.ybu.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j //日志的优先级默认是info
@RestController
@RequestMapping("api/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

//    @GetMapping("list")
//    public R<List<AddressVO>> getAddressList(){
//        List<AddressVO> addressList = addressService.getAddressList();
//        return R.ok(addressList);
//    }

    @GetMapping("getAddresses")
    public R<List<Address>> getAddressByUserIdController(HttpSession session){
        Integer userId = 1;
        List<Address> addressList = addressService.getAddressesByUserId(userId);
        return R.ok(addressList);
    }

    @PostMapping("add")
    public R<AddressDTO> insertAddress(@RequestBody @Validated AddressDTO addressDTO, HttpServletRequest request){
        log.debug("{}",addressDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        UserLoginVO userLoginVO= JwtUtils.parseToken(token);
        /*log.debug("获取到的注册信息：{}",addressDTO);*/
        addressService.addAddress(addressDTO,userLoginVO);
        return R.ok(addressDTO);
    }
}
